//
//  CHEvaluationListFrame.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/14.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHEvaluationListFrame.h"
#define MaxImageCount 6 //最大的图片个数
#define imgViewW 100
// 图片的间距
#define imgMargin 10
#define imgViewH imgViewW
#define TitleFont [UIFont systemFontOfSize:14]
#define DetailTextFont [UIFont systemFontOfSize:12]

@implementation CHEvaluationListFrame

-(void)setModel:(CHEvaluationListModel *)model{
    _model = model;
   
    CGFloat padding = 10;
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    NSDictionary *attr = @{NSFontAttributeName:TitleFont};
    CGSize titleSize = [self boundingRectWithSize:maxSize attributes:attr text:model.company];
    _titleFrm = CGRectMake(padding, padding, titleSize.width, titleSize.height);
    
    _iconFrm = CGRectMake(padding, CGRectGetMaxY(_titleFrm) +padding, 30, 30);
    
    NSDictionary *detailTextAttr = @{NSFontAttributeName:DetailTextFont};
    CGSize phoneSize = [self boundingRectWithSize:maxSize attributes:detailTextAttr text:model.mobilephone];
    
    _phoneFrm = CGRectMake(CGRectGetMaxX(_iconFrm) +padding, CGRectGetMaxY(_titleFrm) +padding , phoneSize.width, 30);
    
    
    _starFrm = CGRectMake(CGRectGetMaxX(_phoneFrm) +padding, CGRectGetMaxY(_titleFrm) +padding+5, 95, 20);
    
    _currentHeight = CGRectGetMaxY(_iconFrm);
    
    if (![model.imgrepairafter isEqualToString:@""]) {
       
        NSArray  *pics = [model.imgrepairafter componentsSeparatedByString:@","];
        
        CGSize photoViewSize = [self sizeWithPhotosCount:pics.count];
        _photosViewFrm = CGRectMake(padding, _currentHeight + padding, UIScreenW - 2*padding, photoViewSize.height);
        _currentHeight = CGRectGetMaxY(_photosViewFrm)+padding;
    }
 
    CGSize contentMaxSize = CGSizeMake(UIScreenW - 2 * padding, MAXFLOAT);
    CGSize contentTextSize = [self boundingRectWithSize:contentMaxSize attributes:attr text:model.evaluatetotledetails];
    _contentTextFrm = CGRectMake(padding, _currentHeight +padding , contentTextSize.width, contentTextSize.height);
    
    CGSize timeSize = [self boundingRectWithSize:maxSize attributes:detailTextAttr text:model.evaluatetime];
    
    _timeFrm = CGRectMake(UIScreenW - padding - timeSize.width, CGRectGetMaxY(_contentTextFrm) +padding, timeSize.width, timeSize.height);
    
    _cellHeight = CGRectGetMaxY(_timeFrm) +padding;
    
}


-(CGSize)sizeWithPhotosCount:(NSInteger)count{
    
    // 如果没有图片，图片size就是0
    if (count == 0) {
        return CGSizeZero;
    }
    
    
    /*
     * 如果有4张图片，显示2列，其它情况显示3列
     */
    NSInteger totalRow = count > 3 ? 2 : 1;
    
    
    
    
    // photosView的宽高
    // photosView的宽高
    CGFloat photosViewW = UIScreenW - 20;
    CGFloat photosViewH = totalRow * imgViewH + (totalRow - 1) * imgMargin;
    
    
    
    NSLog(@"图片view的宽高%f-- %f",photosViewW,photosViewH);
    return CGSizeMake(photosViewW, photosViewH);
    
}



- (CGSize)boundingRectWithSize:(CGSize)maxSize attributes:(NSDictionary *)attr text:(NSString *)text
{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
}




@end
